package com.itheima.web.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Map;

/**
 * @author Xiao
 * @date 2021/8/29
 */

/**
 * Request获取请求参数的通用方式
 * 	获取所有参数Map集合：Map<String,String[]> getParameterMap()
 * 	根据名称获取参数值（数组）：String[] getParameterValues(String name)
 * 	根据名称获取参数值(单个值)：String getParameter(String name)
 */
@WebServlet("/demo05")
public class RequestDemo05 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, String[]> map = req.getParameterMap();
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String[] hobbies = req.getParameterValues("hobby");
        for (Map.Entry<String, String[]> entry : map.entrySet()) {
            String key = entry.getKey();    //获取键
            String[] value = entry.getValue();  //获取值
            System.out.print(key + ":");
            for (String s : value) {
                System.out.print(s + " ");
            }
            System.out.println();
        }
        System.out.println(username);
        System.out.println(password);
        for (String hobby : hobbies) {
            System.out.println(hobby);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
